package com.cxsj.project.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxsj.framework.web.domain.JsonResult;
import com.cxsj.project.system.entity.Product;
import com.cxsj.project.system.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dempsey
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    // 查询所有
    @GetMapping("/detail")
    public JsonResult detail() {
        List<Product> list = productService.list();
        return JsonResult.result(list);
    }

    // 对应id的商品数据
    @GetMapping("/crud/{id}")
    public JsonResult selectById(@PathVariable Integer id) {
        List<Product> list = productService.list(new QueryWrapper<Product>().eq("_id", id));
        return JsonResult.result(list);
    }

    // 商品的6个属性，结果：修改相应商品的属性值
    // put请求("/api/product/crud/change",payload)
    @PutMapping("/crud/change")
    public JsonResult update(@RequestBody Product product) {
        productService.updateById(product);
        return JsonResult.success();
    }

    //传递参数：商品id,结果：删除该商品
    //post请求("/api/product/crud"，id)
    @PutMapping("/crud")
    public JsonResult update(Integer id) {
        productService.removeById(id);
        return JsonResult.success();
    }

}

